﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using NetFwTypeLib;
using Microsoft.Win32;
using System.Windows.Forms;

namespace FWSetupAction
{
    public class Firewall
    {
        protected int[] httpPorts = { 80, 8080 };
        protected INetFwProfile fwProfile;

        public void openFirewall()
        {
            ///////////// Firewall Authorize Application ////////////
            String imageFilename = Application.ExecutablePath;
            setProfile();
            INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications;
            INetFwAuthorizedApplication app =
              (INetFwAuthorizedApplication)getInstance("INetAuthApp");
            app.Name = "VerifyConnection.exe";
            app.ProcessImageFileName = imageFilename;
            try
            {
                apps.Add(app);
                apps = null;

                //////////////// Open Needed Ports /////////////////
                INetFwOpenPorts openports = fwProfile.GloballyOpenPorts;
                foreach (int port in httpPorts)
                {
                    INetFwOpenPort openport =
                      (INetFwOpenPort)getInstance("INetOpenPort");
                    openport.Port = port;
                    openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP;
                    openport.Name = "New Open Port";
                    openports.Add(openport);
                }
                openports = null;
            }
            catch (Exception)
            {
            }
        } // openFirewall

        public void closeFirewall()
        {
            String imageFilename = Application.ExecutablePath;
            setProfile();
            INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications;
            try
            {
                apps.Remove(imageFilename);
                apps = null;
                INetFwOpenPorts ports = fwProfile.GloballyOpenPorts;
                ports.Remove(httpPorts[0], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                ports.Remove(httpPorts[1], NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP);
                ports = null;
            }
            catch (Exception)
            {
            }
        }

        protected void setProfile()
        {
            // Access INetFwMgr
            INetFwMgr fwMgr = (INetFwMgr)getInstance("INetFwMgr");
            INetFwPolicy fwPolicy = fwMgr.LocalPolicy;
            fwProfile = fwPolicy.CurrentProfile;
            fwMgr = null;
            fwPolicy = null;
        }

        protected Object getInstance(String typeName)
        {
            if (typeName == "INetFwMgr")
            {
                Type type = Type.GetTypeFromCLSID(
                new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
                return Activator.CreateInstance(type);
            }
            else if (typeName == "INetAuthApp")
            {
                Type type = Type.GetTypeFromCLSID(
                new Guid("{EC9846B3-2762-4A6B-A214-6ACB603462D2}"));
                return Activator.CreateInstance(type);
            }
            else if (typeName == "INetOpenPort")
            {
                Type type = Type.GetTypeFromCLSID(
                new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}"));
                return Activator.CreateInstance(type);
            }
            else return null;
        }
    }
}